package com.smart.resource;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author chenxingyu
 * @date 2019/8/21 10:21
 */
public class FileSourceExample {
    public static void main(String[] args) {
        try {
            String filePath ="C:/Users/win 10/Desktop/private/spring4x/chapter4-IOC容器/src/main/resources/file1.text";

            //使用系统文件路径方式加载文件
            FileSystemResource res1 = new FileSystemResource(filePath);

            //使用类路径的方式加载文件
            ClassPathResource res2 = new ClassPathResource("file1.text");

            //使用WritableResource接口写资源文件
            OutputStream stream1 = res1.getOutputStream();
            stream1.write("欢迎光临 木马教育".getBytes());
            stream1.close();

            //使用Resource接口读取资源文件
            InputStream ins1 = res1.getInputStream();
            InputStream ins2 = res2.getInputStream();

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int i;
            while ((i=ins1.read())!=-1){
                baos.write(i);
            }
            System.out.println(baos.toString());

            System.out.println("res1:"+res1.getFilename());
            System.out.println("res2:"+res1.getFilename());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
